/**
 *
 */
package com.bm.console.action;

import com.alibaba.fastjson.JSON;
import com.bm.console.business.UploadService;
import com.sys.object.common.ObjException;
import com.sys.object.common.ObjResultCode;
import com.sys.object.common.ObjResultJSON;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 所有向app应该服务端发布的接口
 *
 * @author zxp
 */
@Controller
public class UploadAction {

    @Autowired
    private UploadService uploadService;

    /**
     * 新增动态 Get
     */
    @RequestMapping(value = "/bootUpload.do", method = RequestMethod.GET)
    public String bootUpload() {
        return "/web/bootupload/bootupload";
    }

    /**
     * 新增动态 Get
     */
    @RequestMapping(value = "/bootUploadReturn.do", method = RequestMethod.GET)
    public String bootUploadReturn(Integer place, Integer type, Model model) {
        model.addAttribute("place", place);
        model.addAttribute("type", type);
        return "/web/upload/bootuploadReturn";
    }

    /**
     * 新增动态 Get
     */
    @RequestMapping(value = "/bootUploadCallBackPreview.do", method = RequestMethod.GET)
    public String bootUploadReturn(String callBackPreview, Model model) {
        model.addAttribute("callBackPreview", callBackPreview);
        return "/web/bootupload/bootuploadReturn";
    }

    /**
     * 发布新闻推荐 修改状态APP
     */
    @RequestMapping("/uploadSaveReturnJson.do")
    @ResponseBody
    public ObjResultJSON<String> uploadSaveReturnJson(HttpServletRequest request,
                                                      HttpServletResponse response,
                                                      @RequestParam("files") MultipartFile[] files
    ) {
        ObjResultJSON<String> ret = new ObjResultJSON<String>();

        List<String> list = new ArrayList<>();
        try {
            if (files != null && files.length > 0) {
                StringBuffer buf = new StringBuffer();
                for (MultipartFile myfile : files) {
                    if (!myfile.isEmpty()) {
                        String originalFileName = myfile.getOriginalFilename();
                        String ext = FilenameUtils.getExtension(originalFileName);
                        String newFileName = java.util.UUID.randomUUID().toString() + "." + ext;
                        String uploadPath = "/data/temp";
                        File file = new File(uploadPath, newFileName);
                        FileUtils.copyInputStreamToFile(myfile.getInputStream(), file);
                        String imageUrl = uploadService.upload(file);
                        FileUtils.deleteQuietly(file);
                        if (!StringUtils.isEmpty(imageUrl)) {
                            buf.append(DigestUtils.md5Hex(imageUrl)).append("###").append(imageUrl);
                            list.add(buf.toString());
                            buf.delete(0, buf.length());
                        }
                    }
                }
                if (list != null && list.size() > 0) {
                    ret.setData(JSON.toJSONString(list));
                    ret.setResultCode(ObjResultCode.OK);
                } else {
                    ret.setResultCode(ObjResultCode.DATA_ERROR);
                }
            } else {
                ret.setResultCode(ObjResultCode.DATA_ERROR);
            }
        } catch (Exception e) {
            e.printStackTrace();
            if (e.getClass().equals(ObjException.class)) {
                ret.setResultCode(((ObjException) e).getCode());
            } else {
                ret.setResultCode(ObjResultCode.ERROR, "接口抛异常");
            }
        }
        return ret;
    }


}
